package com.zwb.service.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwb.service.common.CacheConstant;
import com.zwb.service.pojo.Employee;
import com.zwb.service.pojo.Salary;
import com.zwb.service.service.IEmployeeService;
import com.zwb.service.service.ISalaryService;
import com.zwb.service.dto.EmployeeSalaryDto;
import com.zwb.service.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 头发又黑又长
 * @Date 2022/9/11 15:57
 * @email zwb15083976291@163.com
 */
@Api(tags = "员工账套管理")
@CacheConfig(cacheNames = CacheConstant.CACHEABLE_VALUE_3_DAT)
@RestController
@RequestMapping("/salary/sobcfg")
public class SalarySobCfgController {
    @Autowired
    private ISalaryService salaryService;

    @Autowired
    private IEmployeeService employeeService;


    @ApiOperation("获取所有工资账套")
    @Cacheable(key = "'salaryAll'")
    @GetMapping("/")
    public R<List<Salary>> getSalaryAll() {
        return R.success(salaryService.list());
    }


    @ApiOperation("获取员工帐套")
    @GetMapping("/salary")
    public R<Page<Employee>> getEmployeeSalaryPage(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        Page<Employee> page = employeeService.getEmployeeWithSalary(currentPage, size);
        return R.success(page);
    }

    @ApiOperation("更新员工帐套")
    @PutMapping("/")
    public R updateEmployeeSalary(Integer eid, Integer sid) {
        if (ObjectUtils.isEmpty(salaryService.getById(sid))) {
            return R.error("工资帐套不存在");
        }
        employeeService.update(new LambdaUpdateWrapper<Employee>().set(Employee::getSalaryId, sid).eq(Employee::getId, eid));
        return R.success("更新员工账套成功");

    }
}
